define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/index' + location.search,
                    add_url: 'store/add',
                    edit_url: 'store/edit',
                    del_url: 'store/del',
                    multi_url: 'store/multi',
                    import_url: 'store/import',
                    table: 'store',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'store_name', title: __('Store_name'), operate: 'LIKE'},
                        {field: 'store_bannerimage', title: __('Store_bannerimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'type_text', title: __('Type'), operate: false},
                        {field: 'valid_time', title: __('Valid_time'), operate: false},
                        {field: 'store_address', title: __('Store_address'), operate: 'LIKE'},
                        {field: 'store_address', title: __('Store_address'), operate: 'LIKE'},
                        {field: 'store_longitude', title: __('Store_longitude'), operate: false},
                        {field: 'store_latitude', title: __('Store_latitude'), operate: false},
                        {field: 'store_mobile', title: __('Store_mobile'), operate: false},
                        {field: 'store_pwd', title: __('门店密码'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true,
                search: false,

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            function swichToggle(switch1) {
                if (switch1 == '0') {
                    $('#c-switch-text').hide()
                    $('#c-valid_time').show()
                } else {
                    $('#c-switch-text').show()
                    $('#c-valid_time').hide()
                }
            }
            swichToggle('0')
            $(document).on("change", "#c-switch", function(){
                var switch1 = $(this).val()
                swichToggle(switch1)
            });
            Controller.api.bindevent();
        },
        edit: function () {
            function dateFormat(fmt, date) {
              let ret;
              const opt = {
                "Y+": date.getFullYear().toString(), // 年
                "m+": (date.getMonth() + 1).toString(), // 月
                "d+": date.getDate().toString(), // 日
                "H+": date.getHours().toString(), // 时
                "M+": date.getMinutes().toString(), // 分
                "S+": date.getSeconds().toString() // 秒
              };
              for (let k in opt) {
                ret = new RegExp("(" + k + ")").exec(fmt);
                if (ret) {
                  fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
                };
              };
              return fmt;
            }
            function swichToggle(switch1) {
                var curDate = dateFormat('YYYY-mm-dd', new Date()); 
                var foreverDate = '2050-01-01'
                if (switch1 == '0') {
                    $('#c-switch-text').hide()
                    $('#c-valid_time').show()
                    if ($('#c-valid_time').val() === foreverDate) {
                      console.log($('#c-valid_time').val())
                      $('#c-valid_time').val(curDate)
                    }
                } else {
                    $('#c-switch-text').show()
                    $('#c-valid_time').hide()
                }
            }
            
            var switch2 = $('#c-switch').val()
            swichToggle(switch2)
            $(document).on("change", "#c-switch", function(){
                var switch1 = $(this).val()
                swichToggle(switch1)
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
